package com.ruicar.afs.cloud.manage.mq.file;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormClass;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormFieldMapping;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormSealMapping;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormClassService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormFieldMappingService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormSealMappingService;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateDataSendVo;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateRuleVo;
import com.ruicar.afs.cloud.bizcommon.rules.entity.*;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.*;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同模板信息保存
 */
@Component
@AllArgsConstructor
public class SyncTemplateDataProcessor implements AfsMqBizProcessor<TemplateDataSendVo> {

    private final ComAttachmentFileService comAttachmentFileService;

    private final ComPrintFormClassService comPrintFormClassService;

    private final ComPrintFormManageService comPrintFormManageService;

    private final ComPrintFormSealMappingService comPrintFormSealMappingService;

    private final AfsRuleInfoService afsRuleInfoService;

    private final AfsRuleExpressMapper ruleExpressMapper;

    private final AfsRuleExpressAtomMapper afsRuleExpressAtomMapper;

    private final AfsRuleInputMappingMapper afsRuleInputMappingMapper;

    private final AfsRuleItemMapper afsRuleItemMapper;

    private final AfsRuleOperateHisMapper afsRuleOperateHisMapper;

    private final AfsRuleTransExpressMapper afsRuleTransExpressMapper;

    private final AfsRuleResultMappingMapper afsRuleResultMappingMapper;

    private final ComPrintFormFieldMappingService comPrintFormFieldMappingService;

    @Override
    public boolean processMessage(TemplateDataSendVo vo) throws Exception {

        // 获取大小类

        List<ComPrintFormClass> classList = vo.getClassList();
        if (ObjectUtils.isNotEmpty(classList)&&classList.size()>0) {
            for (ComPrintFormClass comPrintFormClass : classList) {
                ComPrintFormClass byId = comPrintFormClassService.getById(comPrintFormClass);
                if (ObjectUtils.isNotEmpty(byId)) {
                    comPrintFormClassService.updateById(comPrintFormClass);
                } else {
                    comPrintFormClassService.save(comPrintFormClass);
                }
            }
        }

        //获取模板

        ComPrintFormManage comPrintFormManage = vo.getComPrintFormManage();
        if (ObjectUtils.isNotEmpty(comPrintFormManage)) {
            ComPrintFormManage byId = comPrintFormManageService.getById(comPrintFormManage);
            if (ObjectUtils.isNotEmpty(byId)) {
                comPrintFormManageService.updateById(comPrintFormManage);
            } else {
                comPrintFormManageService.save(comPrintFormManage);
            }

        }

        //获取文件

        ComAttachmentFile comAttachmentFile = vo.getComAttachmentFile();
        if (ObjectUtils.isNotEmpty(comAttachmentFile)) {
            ComAttachmentFile byId = comAttachmentFileService.getById(comAttachmentFile);
            if (ObjectUtils.isNotEmpty(byId)) {
                comAttachmentFileService.updateById(comAttachmentFile);
            } else {
                comAttachmentFileService.save(comAttachmentFile);
            }
        }

        // 获取映射字段

        List<ComPrintFormSealMapping> comPrintFormSealMappingList = vo.getComPrintFormSealMappingList();
        if (ObjectUtils.isNotEmpty(comPrintFormSealMappingList)&&comPrintFormSealMappingList.size()>0) {
            for (ComPrintFormSealMapping comPrintFormSealMapping : comPrintFormSealMappingList) {
                ComPrintFormSealMapping byId = comPrintFormSealMappingService.getById(comPrintFormSealMapping);
                if (ObjectUtils.isNotEmpty(byId)) {
                    comPrintFormSealMappingService.updateById(comPrintFormSealMapping);
                } else {
                    comPrintFormSealMappingService.save(comPrintFormSealMapping);
                }
            }
        }

        // 获取规则

        List<TemplateRuleVo> ruleVoList = vo.getRuleVoList();
        if (ObjectUtils.isNotEmpty(ruleVoList)&&ruleVoList.size()>0) {
            for (TemplateRuleVo templateRuleVo : ruleVoList) {
                //获取规则信息
                AfsRuleInfo ruleInfo = templateRuleVo.getRuleInfo();
                if (ObjectUtils.isNotEmpty(ruleInfo)) {
                    AfsRuleInfo byId = afsRuleInfoService.getById(ruleInfo);
                    if (ObjectUtils.isNotEmpty(byId)) {
                        afsRuleInfoService.updateById(ruleInfo);
                    } else {
                        afsRuleInfoService.save(ruleInfo);
                    }
                }
                //  获取规则表达式
                List<AfsRuleExpress> ruleExpress = templateRuleVo.getRuleExpress();
                if (ObjectUtils.isNotEmpty(ruleExpress)&&ruleExpress.size()>0) {
                    for (AfsRuleExpress express : ruleExpress) {
                        AfsRuleExpress afsRuleExpress = ruleExpressMapper.selectById(express.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleExpress)) {
                            ruleExpressMapper.updateById(express);
                        } else {
                            ruleExpressMapper.insert(express);
                        }
                    }
                }
                //配置原子
                List<AfsRuleExpressAtom> ruleExpressAtom = templateRuleVo.getRuleExpressAtom();
                if (ObjectUtils.isNotEmpty(ruleExpressAtom)) {
                    for (AfsRuleExpressAtom afsRuleExpressAtom : ruleExpressAtom) {
                        AfsRuleExpressAtom afsRuleExpressAtomContract = afsRuleExpressAtomMapper.selectById(afsRuleExpressAtom.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleExpressAtomContract)) {
                            afsRuleExpressAtomMapper.updateById(afsRuleExpressAtom);
                        } else {
                            afsRuleExpressAtomMapper.insert(afsRuleExpressAtom);
                        }
                    }
                }
                //配置输入映射
                List<AfsRuleInputMapping> ruleInputMapping = templateRuleVo.getRuleInputMapping();
                if (ObjectUtils.isNotEmpty(ruleInputMapping)&&ruleInputMapping.size()>0) {
                    for (AfsRuleInputMapping afsRuleInputMapping : ruleInputMapping) {
                        AfsRuleInputMapping afsRuleInputMappingContract = afsRuleInputMappingMapper.selectById(afsRuleInputMapping);
                        if (ObjectUtils.isNotEmpty(afsRuleInputMappingContract)) {
                            afsRuleInputMappingMapper.updateById(afsRuleInputMapping);
                        } else {
                            afsRuleInputMappingMapper.insert(afsRuleInputMapping);
                        }
                    }
                }
                //配置配置项
                List<AfsRuleItem> ruleItem = templateRuleVo.getRuleItem();
                if (ObjectUtils.isNotEmpty(ruleItem)&&ruleItem.size()>0) {
                    for (AfsRuleItem afsRuleItem : ruleItem) {
                        AfsRuleItem afsRuleItemContract = afsRuleItemMapper.selectById(afsRuleItem.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleItemContract)) {
                            afsRuleItemMapper.updateById(afsRuleItem);
                        } else {
                            afsRuleItemMapper.insert(afsRuleItem);
                        }
                    }
                }
                //配置规则操作人员
                List<AfsRuleOperateHis> ruleOperateHis = templateRuleVo.getRuleOperateHis();
                if (ObjectUtils.isNotEmpty(ruleOperateHis)&&ruleOperateHis.size()>0) {
                    for (AfsRuleOperateHis ruleOperateHi : ruleOperateHis) {
                        AfsRuleOperateHis afsRuleOperateHis = afsRuleOperateHisMapper.selectById(ruleOperateHi.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleOperateHis)) {
                            afsRuleOperateHisMapper.updateById(afsRuleOperateHis);
                        } else {
                            afsRuleOperateHisMapper.insert(ruleOperateHi);
                        }
                    }
                }
                //配置规则转化后的表达式
                List<AfsRuleTransExpress> ruleTransExpress = templateRuleVo.getRuleTransExpress();
                if (ObjectUtils.isNotEmpty(ruleTransExpress)&&ruleTransExpress.size()>0) {
                    for (AfsRuleTransExpress transExpress : ruleTransExpress) {
                        AfsRuleTransExpress afsRuleTransExpress = afsRuleTransExpressMapper.selectById(transExpress.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleTransExpress)) {
                            afsRuleTransExpressMapper.updateById(transExpress);
                        } else {
                            afsRuleTransExpressMapper.insert(transExpress);
                        }
                    }
                }
                //配置输出映射与结果
                List<AfsRuleResultMapping> ruleResultMapping = templateRuleVo.getRuleResultMapping();
                if (ObjectUtils.isNotEmpty(ruleResultMapping)&&ruleResultMapping.size()>0) {
                    for (AfsRuleResultMapping afsRuleResultMapping : ruleResultMapping) {
                        AfsRuleResultMapping afsRuleResultMappingContract = afsRuleResultMappingMapper.selectById(afsRuleResultMapping.getId());
                        if (ObjectUtils.isNotEmpty(afsRuleResultMappingContract)) {
                            afsRuleResultMappingMapper.updateById(afsRuleResultMapping);
                        } else {
                            afsRuleResultMappingMapper.insert(afsRuleResultMapping);
                        }
                    }
                }
            }
        }
        //配置印章映射
        List<ComPrintFormFieldMapping> fieldList = vo.getFieldList();
        if (ObjectUtils.isNotEmpty(fieldList)&&fieldList.size()>0) {
            for (ComPrintFormFieldMapping comPrintFormFieldMapping : fieldList) {
                ComPrintFormFieldMapping byId = comPrintFormFieldMappingService.getById(comPrintFormFieldMapping);
                if (ObjectUtils.isNotEmpty(byId)) {
                    comPrintFormFieldMappingService.updateById(comPrintFormFieldMapping);
                } else {
                    comPrintFormFieldMappingService.save(comPrintFormFieldMapping);
                }
            }
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_CASE_CONTRACT_CTM_NEW_TEMPLATE;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }
}
